﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using TracyGUI.UIComponent;
using Tracy;
using Tracy.DataModel;
using System.Xml;
using System.IO;

namespace TracyGUI
{
    public partial class FormMain
    {
        
        private EnhancedTabControl<TabPageEntry> _tabControlContent;

        private void InitEntryTabs()
        {
            _tabControlContent = new EnhancedTabControl<TabPageEntry>();
            splitContainerV.Panel2.Controls.Add(_tabControlContent);
            _tabControlContent.Dock = DockStyle.Fill;
            _tabControlContent.AllowDrop = true;
            _tabControlContent.PageRemoved += new EventHandler<TabPageEvent>(tabControlContent_PageRemoved);
        }

        void tabControlContent_PageRemoved(object sender, TabPageEvent e)
        {
            if (e.TabPage == _selectedEntryPage) _selectedEntryPage = null;
            if (e.TabPage == _searchEntryPage) _searchEntryPage = null;
        }

        

        /// <summary>
        /// Create new EntryTab
        /// </summary>
        /// <param name="entryName">'All' - All Resources; 'Search' - Search Result; 'Ultra Explorer' - Ultra Explorer filter result; Otherwise - Entry ID</param>
        private TabPageEntry CreateEntryTab(string entryName, string parameter = null)
        {
            string tabText = GetEntryTabText(entryName, parameter);
            if (tabText == null) return null;
            TabPageEntry newTab = new TabPageEntry(entryName, tabText);
            newTab.Tag = parameter;
            AddFilesTab(entryName, newTab.TabControl, parameter);

            if (entryName != TabPageEntry.TABPAGE_NAME_UE)
            {
                AddResourcesTab(entryName, newTab.TabControl, parameter);
            }
            return newTab;
        }

        /// <summary>
        /// Remove related tabs
        /// </summary>
        /// <param name="entry"></param>
        private void RemoveEntryTab(Entry entry)
        {
            List<TabPageEntry> list = new List<TabPageEntry>();
            foreach (TabPageEntry tab in _tabControlContent.TabPages)
            {
                if (tab.Name == entry.ID)
                {
                    list.Add (tab);   
                }
            }
            foreach (TabPageEntry tab in list)
            {
                if (tab == _selectedEntryPage) _selectedEntryPage = null;
                _tabControlContent.TabPages.Remove(tab);
            }
        }

        private string GetEntryTabText(string entryName, string parameter = null)
        {
            if (entryName == TabPageEntry.TABPAGE_NAME_ALL)
            {
                return "All Entries";
            }
            else if (entryName == TabPageEntry.TABPAGE_NAME_SEARCH)
            {
                return "Search Result - " + parameter;
            }
            else if (entryName == TabPageEntry.TABPAGE_NAME_UE)
            {
                return TabPageEntry.TABPAGE_NAME_UE;
            }
            else
            {
                Entry entry = TracyFacade.Instance.EntryProvider.GetItemByID(entryName);
                if (entry == null) return null;
                return entry.NameWithUnwatchedCount;
            }
        }

        private void LoadDataToEntryTab(TabPageEntry tab, string entryName, string parameter = null, bool clearView = true)
        {
            tab.Name = entryName;
            tab.Text = GetEntryTabText(entryName, parameter);
            tab.Tag = parameter;
            TabPageFiles fileTab = tab.GetTabPage(TabPageEntry.TABPAGE_NAME_FILES) as TabPageFiles;
            LoadDataToFileTab(fileTab, entryName, parameter, clearView);

            if (entryName != TabPageEntry.TABPAGE_NAME_UE)
            {
                TabPageResources resTab = tab.GetTabPage(TabPageEntry.TABPAGE_NAME_RESOURCES) as TabPageResources;
                LoadDataToResourceTab(resTab, entryName, parameter, clearView);
            }
        }
        private List<TabPageEntry> GetRelatedEntryTabs(string entryID)
        {
            List<TabPageEntry> rtn = new List<TabPageEntry>();
            foreach (TabPageEntry tab in _tabControlContent.TabPages)
            {
                if (tab.Name == TabPageEntry.TABPAGE_NAME_ALL || tab.Name == entryID)
                {
                    rtn.Add(tab);
                }
            }
            return rtn;
        }

    }
}
